package com.societyGames.flashForms
{
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Rectangle;

public class HorizontalSlidingDoorContainer extends Sprite
{
  private var _desiredHeight:Number;
  private var _desiredWidth:Number;

  private var _leftPercent:Number = 0;
  private var _leftItem:DisplayObject;
  private var _leftSplitter:Splitter;

  private var _centerItem:DisplayObject;

  private var _rightPercent:Number = 0;
  private var _rightItem:DisplayObject;
  private var _rightSplitter:Splitter;

  public function HorizontalSlidingDoorContainer(leftSplitterSprite:DisplayObject, rightSplitterSprite:DisplayObject, leftRightCursor:DisplayObject, leftPercent:Number = 0.33, rightPercent:Number = 0.33)
  {
    this._leftSplitter = new Splitter(leftSplitterSprite, leftRightCursor);
    this._leftSplitter.y = 0;
    this._leftSplitter.bounds = new Rectangle();
    this._leftSplitter.droppedEventObject.addEventListener(leftDroppedHandler);
    this.addChild(this._leftSplitter);

    this._rightSplitter = new Splitter(rightSplitterSprite, leftRightCursor);
    this._rightSplitter.y = 0;
    this._rightSplitter.bounds = new Rectangle();
    this._rightSplitter.droppedEventObject.addEventListener(rightDroppedHandler);
    this.addChild(this._rightSplitter);

    this._rightPercent = rightPercent;
    this.rightPercent = rightPercent;
    this.leftPercent = leftPercent;
  }

  public function get leftItem():DisplayObject
  {
    return this._leftItem;
  }

  public function set leftItem(value:DisplayObject):void
  {
    if (this._leftItem != value)
    {
      if (this._leftItem != null)
      {
        this.removeChild(this._leftItem);
      }
      this._leftItem = value;
      if (this._leftItem != null)
      {
        this._leftItem.y = 0;
        this._leftItem.height = this._desiredHeight;
        this.addChildAt(this._leftItem, 0);
        recalculateWidth();
      }
    }
  }

  public function get centerItem():DisplayObject
  {
    return this._centerItem;
  }

  public function set centerItem(value:DisplayObject):void
  {
    if (this._centerItem != value)
    {
      if (this._centerItem != null)
      {
        this.removeChild(this._centerItem);
      }
      this._centerItem = value;
      if (this._centerItem != null)
      {
        this._centerItem.y = 0;
        this._centerItem.height = this._desiredHeight;
        this.addChildAt(this._centerItem, 0);
        recalculateWidth();
      }
    }
  }

  public function get rightItem():DisplayObject
  {
    return this._rightItem;
  }

  public function set rightItem(value:DisplayObject):void
  {
    if (this._rightItem != value)
    {
      if (this._rightItem != null)
      {
        this.removeChild(this._rightItem);
      }
      this._rightItem = value;
      if (this._rightItem != null)
      {
        this._rightItem.y = 0;
        this._rightItem.height = this._desiredHeight;
        this.addChildAt(this._rightItem, 0);
        recalculateWidth();
      }
    }
  }

  public function set rightPercent(value:Number):void
  {
    if (value != this._rightPercent)
    {
      if (value > 1)
      {
        value = 1;
      }
      this._rightPercent = value;
      if (this._leftPercent + this._rightPercent > 1)
      {
        this._leftPercent = 1 - this._rightPercent;
      }
      this.recalculateWidth();
    }
  }

  public function set leftPercent(value:Number):void
  {
    if (value != this._leftPercent)
    {
      if (value > 1)
      {
        value = 1;
      }
      this._leftPercent = value;
      if (this._leftPercent + this._rightPercent > 1)
      {
        this._rightPercent = 1 - this._leftPercent;
      }
      this.recalculateWidth();
    }
  }

  public override function set height(value:Number):void
  {
    if (value != this._desiredHeight)
    {
      this._desiredHeight = value;
      if (this._leftItem != null)
      {
        this._leftItem.height = value;
      }
      this._leftSplitter.height = value;
      if (this._centerItem != null)
      {
        this._centerItem.height = value;
      }
      this._rightSplitter.height = value;
      if (this._rightItem != null)
      {
        this._rightItem.height = value;
      }
    }
  }

  private function recalculateWidth():void
  {
    var usableWidth:Number = this._desiredWidth - this._leftSplitter.width - this._rightSplitter.width;
    var leftItemWidth:Number = usableWidth * this._leftPercent;
    if (this._leftItem != null)
    {
      this._leftItem.x = 0;
      this._leftItem.width = leftItemWidth;
    }
    this._leftSplitter.x = leftItemWidth;
    this._leftSplitter.bounds.x = 0;
    this._leftSplitter.bounds.y = 0;
    this._leftSplitter.bounds.width = this._desiredWidth - this._leftSplitter.width - this._rightSplitter.width;
    this._leftSplitter.bounds.height = 0;

    this._rightSplitter.x = this._desiredWidth - this._rightSplitter.width - (usableWidth * this._rightPercent);

    if (this._centerItem != null)
    {
      this._centerItem.x = this._leftSplitter.x + this._leftSplitter.width;
      this._centerItem.width = this._rightSplitter.x - this._centerItem.x;
    }

    this._rightSplitter.bounds.x = this._leftSplitter.width;
    this._rightSplitter.bounds.y = 0;
    this._rightSplitter.bounds.width = this._desiredWidth - this._leftSplitter.width - this._rightSplitter.width;
    this._rightSplitter.bounds.height = 0;
    if (this._rightItem != null)
    {
      this._rightItem.x = this._rightSplitter.x + this._rightSplitter.width;
      this._rightItem.width = this._desiredWidth - this._rightItem.x;
    }
  }

  public override function set width(value:Number):void
  {
    if (value != this._desiredWidth)
    {
      this._desiredWidth = value;
      recalculateWidth();
    }
  }

  private function leftDroppedHandler(event:Event):void
  {
    this.leftPercent = this._leftSplitter.x / (this._desiredWidth - this._leftSplitter.width - this._rightSplitter.width);
  }

  private function rightDroppedHandler(event:Event):void
  {
    this.rightPercent = (this._desiredWidth - this._rightSplitter.x) / (this._desiredWidth - this._leftSplitter.width - this._rightSplitter.width);
  }
}
}